/* Connect_Four.as
 * Takes in the checker and button class and utilizes them to make a Connect Four game
 *
 */
package {

        import Button;
        import Checker;
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.geom.Point;
        import flash.utils.Timer;
        import flash.events.TimerEvent;
        import flash.text.TextField;
		//import flash.text;

        public class Connect_Four extends Sprite {
                
                //Decare our member variables
                private var checker       : Checker;
                private var currentPlayer : int   = 1;
                private var checkerCount  : int   = 0;
                private var board         : Array = new Array();
                private var timer         : Timer;
                private var endGame       : Boolean;
				
				
               
			   
                //Call the main function that calls createDisplayList()
                public function Connect_Four() {
                        
                        //We do this so we can call createDisplayList() again in the reset() function to make it easy
                        createDisplayList();

                }
                
                //Generates the buttons used for selecting columns
                private function createDisplayList() : void 
				{
                        
                        //We use a for loop for this section since we're making multiple copies of the same button
                        for ( var i:int = 0; i < 7; ++i ) 
						{

                                var button : Button = new Button( "Move", i, 50, 30, buttonClicked );
                                this.addChild(button);
                                button.x = i * 70 + 35;
                                button.y = 500;

                        }
						
						//Reset button; not sure how to work it
						var reset : Button = new Button("RESET",0,75,30,reset);
						this.addChild(reset);
						reset.x = 550;
						reset.y = 250;
						
                        
                        //Call the createBoardArray() function
                        createBoardArray();
                        endGame = false;
                        
                }
                
                //The buttonClicked function is where most of the coding stems from because it's required for it to do various things
                private function buttonClicked( e : Event ) : void {

                        var button  : Button = Button( e.currentTarget );
                        
                        //Make it impossible to drop checkers if endGame = true
                        if ( endGame ) {
                                
                                return;
                                
                        }
                        else {
                                
                                for ( var i : int = 5; i >= 0; i-- ) {
                                        
                                        //If there's an empty space in a column, fill it.
                                        if ( board[i][button.get_value()] == 0 ) {
                                                
                                                //Assigns the value of the space equal to the current player's number, p1 = 1, p2 = 2.
                                                board[i][button.get_value()] = currentPlayer;
                                                checker = new Checker( currentPlayer, new Point( button.get_value() * 35 + 30, i * 65 + 85 ) );
                                                checkerCount += 1; //Increase the checker count by one
                                                
                                                //Check the win using a point at ( row, column ), should probably change it to something else maybe.
                                                if ( checkWin( new Point( i, button.get_value() ) ) ) {
                                                        
                                                        //Call the same function as Tie game but put different text on it -> Reference Button.as -> new timer with x delay that runs once that calls reset() or code into a button. Set endGame to true.
                                                        trace( "Player " + currentPlayer + " Wins!" );
														//var sum :TextField
														var myText:TextField = new TextField();
														myText.text = "Player " + currentPlayer + " Wins!"  ;
														this.addChild(myText);
														myText.textColor = 0x0000FF;
														myText.x = 550;
														myText.y = 100;
														
														
														
														
                                                        
                                                }
                                                if ( checkerCount == 42 && checkWin( new Point( i, button.get_value() ) ) == false ) {
                                
                                                        //Call the same function as Win game but put different text on it -> Reference Button.as -> new timer with x delay that runs once that calls reset() or code into a button. Set endGame to true.
                                                        trace( "Tie game!" )
                                
                                                }
                                                //If neither conditions are met, we just swap players
                                                else {
                                                        
                                                        swapPlayer();
                                                        
                                                }
                                                
                                                //Add the checker to the stage
                                                this.addChild( checker );
                                                //this.swapChild( the board )
                                                //End the loop
                                                return
                                                
                                        }
                                
                                }
                        
                        }

                }
                
                //Create the matrix for the board spaces
                private function createBoardArray() : void 
				{
                        
                        //Initialize a for loop to make things easy
                        for( var i : int = 0; i < 6; ++i ) 
						{
                                
                                //board[i] = new array() is what that basically says
                                board[i] = []
                                
                                for( var j : int=0; j < 7; ++j ) 
								{
                                        
                                        //Put 0 as the default value of each space
                                        board[i][j] = 0;
                                
                                }
                        }
                }
                
                //Check the win, if it's a win, return true, else return false
                private function checkWin( point : Point ) : Boolean {
                        
                        if ( horizontalCheck  ( point ) ) return true;
                        if ( leftDiagonalCheck( point ) ) return true;
						if ( verticalCheck    ( point ) ) return true;
                        if ( rightDiagonalCheck( point ) ) return true;
						/* if vertical
                         *   for row - 1; i >= 0, etc. -> if board[i][column] != player -> etc.
                         *   for row + 1; i <  6, etc. -> if board[i][column] != player -> etc.
                         * end
                         *
                         * if right diagonal
                         *   while loop with variables { row - 1, column + 1 } -> row--; column++;
                         *   while loop with variables { row + 1, column - 1 } -> row++; column--;
                         * end
                         *
                         * else
                         */
                        return false;
                        
                }
                
                private function horizontalCheck( point : Point) : Boolean {
                        
                        var row     : int = point.x;
                        var column  : int = point.y;
                        var player  : int = board[row][column];
                        var counter : int = 1;
                        
                        //For loop that checks columns in the right direction and adds to the counter variable
                        for ( var i : int = column - 1; i >= 0; --i ) 
						{
                                
                                if ( board[row][i] != player ) 
								{
                                        
                                        //Break the current loop
                                        break;
                                        
                                }
                                
                                counter++;
                                
                        }
                        
                        //For loop that checks columns in the left direction and adds to the counter variable
                        for ( var j : int = column + 1; j < 7; ++j ) 
						{
                                
                                if ( board[row][j] != player ) 
								{
                                        
                                        //Break the current loop
                                        break;
                                        
                                }
                                
                                counter++;
                                
                        }
                        
                        if ( counter >= 4 ) {
                                
                                return true;
                                
                        }
                        else {
                                
                                return false;
                                
                        }
                        
                }
				
				private function verticalCheck( point : Point) : Boolean
				{
                        
                        var row     : int = point.x;
                        var column  : int = point.y;
                        var player  : int = board[row][column];
                        var counter : int = 1;
                        
                        //For loop that checks columns in the right direction and adds to the counter variable
                        for ( var i : int = row - 1; i >= 0; --i ) 
						{
                                
                                if ( board[i][column] != player ) 
								{
                                        
                                        //Break the current loop
                                        break;
                                        
                                }
                                
                                counter++;
                                
                        }
                        
                        //For loop that checks columns in the left direction and adds to the counter variable
                        for ( var j : int = row + 1; j < 6; ++j ) 
						{
                                
                                if ( board[j][column] != player ) 
								{
                                        
                                        //Break the current loop
                                        break;
                                        
                                }
                                
                                counter++;
                                
                        }
                        
                        if ( counter >= 4 ) {
                                
                                return true;
                                
                        }
                        else {
                                
                                return false;
                                
                        }
                        
                }
               
                private function leftDiagonalCheck( point : Point ) : Boolean 
				{
                        
                        //We need to subtract one so it doesn't declare a winner after placing a second checker in the left diagonal direction
                        var row     : int = point.x - 1;
                        var column  : int = point.y - 1;
                        //However, player must be the same as the point given
                        var player  : int = board[point.x][point.y];
                        var counter : int = 1;
                        
                        //Initialize a while loop that runs as long as rows > 0 and columns > 0, it checks checkers downward in the left diagonal direction
                        while ( row >= 0 && column >= 0 ) {
                                
                                if ( board[row][column] == player ) {
                                        
                                        //Shift it down and right one
                                        row--;
                                        column--;
                                        counter++;
                                        
                                }
                                else {
                                        
                                        //Break the loop
                                        break;
                                        
                                }
                                
                        }
                        
                        //Have to put new values into row and column since the previous while loop reduced them. 1 has to be added so it doesn't mistake the win, just like with the last one
                        row = point.x + 1;
                        column = point.y + 1;
                        
                        while ( row < 6 && column < 7 ) {
                                
                                if ( board[row][column] == player ) {
                                        
                                        //Shift if up and left
                                        row ++;
                                        column++;
                                        counter++;
                                        
                                }
                                else {
                                        
                                        //Break the loop
                                        break
                                        
                                }
                                
                        }
                        
                        if ( counter >= 4 ) {
                                
                                return true;
                                
                        }
                        else {
                                
                                return false;
                                
                        }
                        
                }
				private function rightDiagonalCheck( point : Point ) : Boolean 
				{
                        
                        //We need to subtract one so it doesn't declare a winner after placing a second checker in the left diagonal direction
                        var row     : int = point.x - 1;
                        var column  : int = point.y + 1;
                        //However, player must be the same as the point given
                        var player  : int = board[point.x][point.y];
                        var counter : int = 1;
                        
                        //Initialize a while loop that runs as long as rows > 0 and columns > 0, it checks checkers downward in the left diagonal direction
                        while ( row >= 0 && column < 7 ) {
                                
                                if ( board[row][column] == player ) {
                                        
                                        //Shift it down and right one
                                        row--;
                                        column++;
                                        counter++;
                                        
                                }
                                else {
                                        
                                        //Break the loop
                                        break;
                                        
                                }
                                
                        }
                        
                        //Have to put new values into row and column since the previous while loop reduced them. 1 has to be added so it doesn't mistake the win, just like with the last one
                        row = point.x + 1;
                        column = point.y - 1;
                        
                        while ( row < 6 && column >= 0 ) {
                                
                                if ( board[row][column] == player ) {
                                        
                                        //Shift if up and left
                                        row ++;
                                        column--;
                                        counter++;
                                        
                                }
                                else {
                                        
                                        //Break the loop
                                        break
                                        
                                }
                                
                        }
                        
                        if ( counter >= 4 ) {
                                
                                return true;
                                
                        }
                        else {
                                
                                return false;
                                
                        }
                        
                }
                
                //Reset the board
                private function reset(e:Event) : void {
                        //var reset  : Button = Button("reset");
                        //Reset the value of each space to 0
                        for ( var i : int = 0; i < 6; ++i ) {
                                
                                for ( var j : int = 0; j < 7; ++j ) {
                                        
                                        board[i][j] = 0;
                                        
                                }
                                
                        }
                        
                        //Remove all children from the stage, this clears the buttons as well
                        while ( this.numChildren != 0 ) 
						{
                                
                                //Index 0 is all children
                                this.removeChildAt( 0 );
                                
                        }
                        
                        //Reset the checker count
                        checkerCount = 0;
                        //Redraw the buttons and initialize the board again
                        createDisplayList();
                        
                }
                
                //Swap the current player, kind of self-explanatory
                private function swapPlayer() : void {

                        if ( currentPlayer == 1 )
                        {

                                currentPlayer = 2;

                        }
                        else
                        {

                                currentPlayer = 1;

                        }

                }

        }

}